package com.project.frba.bifront.server.rpc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.project.frba.bi.BiUtil;
import com.project.frba.bi.descriptor.Descriptor;
import com.project.frba.bi.descriptor.DescriptorsLoader;
import com.project.frba.bi.queueing.CubeMediator;
import com.project.frba.bi.queueing.OlapDAO;
import com.project.frba.bi.queueing.OlapQueryBuilder;
import com.project.frba.bifront.client.model.Cube;
import com.project.frba.bifront.client.model.CubePosition;
import com.project.frba.bifront.client.model.DataNode;
import com.project.frba.bifront.client.model.Filter;
import com.project.frba.bifront.client.model.SpansCounter;
import com.project.frba.bifront.client.rpc.CubeServices;

public class CubeServicesImpl extends RemoteServiceServlet implements
		CubeServices {

	public Cube getCube(String descriptorName, List<String> cProps, List<String> rProps, List<Filter> filters) {
		
			Descriptor descriptor = DescriptorsLoader.getDescriptorByName(descriptorName);
			List<String> allProps = new ArrayList<String>();
			allProps.addAll(cProps);
			allProps.addAll(rProps);
			Cube cube = CubeMediator.getDataWithSpring(descriptor, allProps, filters);
			//Cube cube = CubeMediator.getDataWithSpring(descriptor, allProps, filters);
			
			cube.buildColumnTree(cProps);
			cube.buildRowTree(rProps);
			
			return cube;
		
	}
	
	public List<String> getValuesForProperty(String descriptorName, String propertyName) {
		Descriptor descriptor = DescriptorsLoader.getDescriptorByName(descriptorName);
		OlapQueryBuilder queryBuilder = new OlapQueryBuilder(descriptor);
		String query = queryBuilder.getAllPropertyValues(propertyName);
		System.out.println(query);
		ApplicationContext context = BiUtil.getApplicationContext();
		OlapDAO dao = (OlapDAO)context.getBean("olapDao");
		
		return dao.getValuesForProperty(query, descriptor.getPropertyByName(propertyName).getType());
	}
	
	
}
